package com.guyaning.deyson_core.core.net.download;

import android.os.AsyncTask;

import com.guyaning.deyson_core.core.net.RestCreator;
import com.guyaning.deyson_core.core.net.callback.IError;
import com.guyaning.deyson_core.core.net.callback.IFailure;
import com.guyaning.deyson_core.core.net.callback.IRequest;
import com.guyaning.deyson_core.core.net.callback.ISuccess;

import java.util.WeakHashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/4/23.
 */

public class DownLoadHandler {

    private final String URL;

    private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();

    private final String DOWNLOAD_DIR;

    private final String EXTENSION;

    private final String NAME;

    private final IRequest REQUEST;

    private final ISuccess SUCCESS;

    private final IError ERROR;

    private final IFailure FAILURE;

    public DownLoadHandler(String url,
                           String download_dir,
                           String extension,
                           String name,
                           IRequest request,
                           ISuccess success,
                           IError error,
                           IFailure failure) {
        this.URL = url;
        this.DOWNLOAD_DIR = download_dir;
        this.EXTENSION = extension;
        this.NAME = name;
        this.REQUEST = request;
        this.SUCCESS = success;
        this.ERROR = error;
        this.FAILURE = failure;
    }

    public final void handlerDownLoad() {
        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }

        RestCreator.getResetService().download(URL, PARAMS)
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                        if (response.isSuccessful()) {

                            final ResponseBody responseBody = response.body();

                            SaveFileTask saveFileTask = new SaveFileTask(REQUEST, SUCCESS);

                            saveFileTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, DOWNLOAD_DIR, EXTENSION, responseBody);

                            //这里一定要做一下判断，否则会文件下载不全
                            if (saveFileTask.isCancelled()) {
                                if (REQUEST != null) {
                                    REQUEST.onRequestEnd();
                                }
                            }

                        } else {
                            if (ERROR != null) {
                                ERROR.onError(response.code(), response.message());
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                           if(FAILURE!=null){
                               FAILURE.onFailure(t.getMessage());
                           }
                    }
                });
    }
}
